import { defineStore } from 'pinia'
import config from '@/config/main'

export const useToastStore = defineStore('toast', {
    state: () => {
        return {
            id: 0,
            messages: [
                // { id: 1, type: 'SUCCESS', content: '第一条消息！', delay: 600000 },
                // { id: 2, type: 'WARNING', content: '第二条消息！', delay: 600000 },
            ] // 消息列表 [{ id, type, content, delay }]
        }
    },
    actions: {
        success(content, delay = 3000) {
            this.messages.push({
                type: 'SUCCESS',
                content,
                delay: delay || config.messageTipDelay,
                id: this.id++
            })
        },
        error(content, delay = 3000) {
            this.messages.push({
                type: 'ERROR',
                content,
                delay: delay || config.messageTipDelay,
                id: this.id++
            })
        },
        warning(content, delay = 3000) {
            this.messages.push({
                type: 'WARNING',
                content,
                delay: delay || config.messageTipDelay,
                id: this.id++
            })
        },
        info(content, delay = 3000) {
            this.messages.push({
                type: 'INFO',
                content,
                delay: delay || config.messageTipDelay,
                id: this.id++
            })
        },
        del(id) {
            const findIndex = this.messages.findIndex((item) => item.id == id)
            if (findIndex >= 0) {
                this.messages.splice(findIndex, 1)
            }
        }
    }
})
